home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / system.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  3.8 KB  |  175 lines

  1. /* system.c -- The code needed in order to start child processes.  */
  2.  
  3. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Written by Tudor Hulubei and Andrei Pitis.  */
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27.  
  28. #ifdef HAVE_STDLIB_H
  29. #include <stdlib.h>
  30. #else /* !HAVE_STDLIB_H */
  31. #include "ansi_stdlib.h"
  32. #endif /* !HAVE_STDLIB_H */
  33.  
  34. #include <sys/types.h>
  35.  
  36. #ifdef HAVE_UNISTD_H
  37. #include <unistd.h>
  38. #endif /* HAVE_UNISTD_H */
  39.  
  40. #include <errno.h>
  41.  
  42. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  43. #if !defined (errno)
  44. extern int errno;
  45. #endif /* !errno */
  46.  
  47. #include "xmalloc.h"
  48. #include "xio.h"
  49. #include "tty.h"
  50. #include "signals.h"
  51. #include "inputline.h"
  52. #include "system.h"
  53. #include "misc.h"
  54.  
  55.  
  56. char *stdout_log_name = NULL;
  57. char *stderr_log_name = NULL;
  58.  
  59. extern int signals_status;
  60. extern char *screen;
  61.  
  62. /* This should be read from the configuration file here, in system.c.
  63.    We will be able to do so when the hooks package will be ready.  */
  64. extern char *TempDirectory;
  65.  
  66.  
  67. char il_read_char PROTO ((char *, char *, int));
  68.  
  69.  
  70. int
  71. start(cmd, hide)
  72.     char *cmd;
  73.     int hide;
  74. {
  75.     int child_exit_code;
  76.     FILE *stdout_log, *stderr_log;
  77.  
  78.     if (hide)
  79.     {
  80.     /* It is not a good idea to close the terminal descriptors.
  81.        We might loose.  Suppose git has been started as root + su
  82.        user.  git will run as user, but the tty will belong to
  83.        root.  git will be able to use it at the beginning, since
  84.        it inherits the descriptors (they are already opened), but
  85.        once the descriptors closed, it will not be able to open
  86.        them again.  Therefore we don't close the descriptors but
  87.        instead save them (using the dup() system call) and restore
  88.        them later.  */
  89.  
  90.     int old_stdout = dup(1);
  91.     int old_stderr = dup(2);
  92.  
  93.     close(1);
  94.     close(2);
  95.  
  96.     stdout_log = fopen(stdout_log_name, "w");
  97.     stderr_log = fopen(stderr_log_name, "w");
  98.  
  99.     restore_signals();
  100.     signals_dfl();
  101.     child_exit_code = system(cmd);
  102.     signals(signals_status);
  103.     ignore_signals();
  104.  
  105.     fclose(stdout_log);
  106.     fclose(stderr_log);
  107.  
  108.     dup(old_stdout);
  109.     dup(old_stderr);
  110.  
  111.     close(old_stdout);
  112.     close(old_stderr);
  113.     }
  114.     else
  115.     {
  116.     tty_set_mode(TTY_CANONIC);
  117.     tty_defaults();
  118.     tty_put_screen(screen);
  119.  
  120.     restore_signals();
  121.     signals_dfl();
  122.     child_exit_code = system(cmd);
  123.     signals(signals_status);
  124.     ignore_signals();
  125.  
  126.     xwrite(1, "\n\n", 2);
  127.     tty_set_mode(TTY_NONCANONIC);
  128.     tty_defaults();
  129.     }
  130.  
  131.     return child_exit_code;
  132. }
  133.  
  134.  
  135. void
  136. removelog()
  137. {
  138.     if (stdout_log_name)
  139.     unlink(stdout_log_name);
  140.  
  141.     if (stderr_log_name)
  142.     unlink(stderr_log_name);
  143. }
  144.  
  145.  
  146. void
  147. display_errors(command)
  148.     char *command;
  149. {
  150.     FILE *stderr_log = fopen(stderr_log_name, "r");
  151.  
  152.     if (stderr_log == NULL)
  153.     {
  154.     size_t buf_len = 32 + strlen(stderr_log_name);
  155.     char *buf      = xmalloc(buf_len);
  156.  
  157.     sprintf(buf, "%s: cannot open log file %s", command, stderr_log_name);
  158.  
  159.     il_read_char(buf, NULL, IL_MOVE | IL_BEEP | IL_SAVE | IL_ERROR);
  160.     xfree(buf);
  161.     }
  162.     else
  163.     {
  164.     char *buf = xmalloc(2048 + 1);
  165.  
  166.     while (fgets(buf, 2048 + 1, stderr_log))
  167.         if (il_read_char(buf, NULL, IL_MOVE | IL_ERROR) == 0)
  168.         break;
  169.  
  170.     xfree(buf);
  171.     }
  172.  
  173.     fclose(stderr_log);
  174. }
  175.